package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Oppo;
import com.manger.model.Project;
import com.manger.model.VIVO;
import com.manger.utils.CopyFiles;
import com.manger.utils.DetNameEnum;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName VivoCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/1 16:00
 * @Version 1.0
 */
public class VivoCaseFilesFormat implements CaseFilesFormat {


    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        String mFilePath = extractDir + "/" + project.getDebtName() + "/";
        File mFile = new File(mFilePath);
        String excelPath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        Map<String, List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelPath), VIVO.class).get();
        if (map.keySet().size() <= 0) {
            System.out.println("没有解析到委托信息！");
            return;
        }
        if (!mFile.exists()) {
            System.out.println("没有检测到案件信息！");
            return;
        }
        List<VIVO> vivoList = (List<VIVO>) map.get(map.keySet().iterator().next());

        File[] files = mFile.listFiles();
        Map<String, String> tempMap = new HashMap<>();
        for (File file : files) {
            File[] userCaseDirs = file.listFiles();
            for (File userCaseDir : userCaseDirs) {
                if (!userCaseDir.isDirectory()) {
                    continue;
                }
                File[] userCaseFilesDirs = userCaseDir.listFiles();
                for (File userCaseFileDir : userCaseFilesDirs) {
                    System.out.println(userCaseFileDir.getName());
                    File[] userCaseFiles = userCaseFileDir.listFiles();
                    VIVO vivo = null;
                    String openId = userCaseFileDir.getName().split("_")[1];
                    for(VIVO v : vivoList) {
                        if(v.getOpenId().equals(openId)){
                            vivo = v;
                            break;
                        }
                    }
                    if(vivo == null){
                        continue;
                    }

                    String tempTargetFileDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                            + "/" + vivo.getUserName() + "-" + vivo.getIdCard();
                    if(userCaseFileDir.isFile()){
                        userCaseFiles = userCaseFilesDirs;
                    }
                    for (File tempFile : userCaseFiles) {
                        if (tempFile.getName().endsWith(".xlsx")) {
                            continue;
                        }
                        CopyFiles.copyFileToDir(tempFile.getCanonicalPath(), tempTargetFileDir + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
                    }
                    CopyFiles.copyBaseFiles(tempTargetFileDir, project);
                    tempMap.put(vivo.getIdCard(), tempTargetFileDir);
                    System.out.println("整理案件成功：【" + userCaseFileDir.getName() + "】");
                    if(userCaseFileDir.isFile()){
                        break;
                    }
                }
            }
        }
        System.out.println("拷贝材料信息");
        CopyFiles.copyOtherMaterialFileToDir(extractDir, tempMap);
    }

    private File getCustomExcelFile(File[] files) {
        for (File file : files) {
            if (file.getName().contains("委托客户信息")) {
                return file;
            }
        }
        return null;
    }
    public static void main(String[] args) throws Exception {
        VivoCaseFilesFormat vivoCaseFilesFormat = new VivoCaseFilesFormat();
        String targetDir = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\vivo";
        Project project = new Project();
        project.setDebtName(DetNameEnum.VIVO.getDesc());
        vivoCaseFilesFormat.format(extractDir, targetDir, project);
    }


}
